var ShowTaskData = {};

var sort = null;
var rateTime = null;
var searchType = null;
var phoneOrder = null;
var realName = null;

app.controller('indexCtrl', ['$scope', function ($scope) {
	var needNewPagination = true;
	
	$("#minMoney").val("");
	$("#maxMoney").val("");
	refreshShowTask(1, sort, rateTime, searchType, phoneOrder, realName);
	getCzRecords();
	getPublicForum();
	
	function refreshShowTask(pageNo, sort, rateTime, searchType, phoneOrder, realName) {
		var minMoney = $.trim($("#minMoney").val());
		var maxMoney = $.trim($("#maxMoney").val());
		var range = minMoney +","+ maxMoney;
		var autoFilter = $(".auto-filter-box").val();
		
		var param = {
			pageNo: pageNo,
			pageCount: 15,
			sort: sort,
			range: range,
			rateTime: rateTime,
			searchType: searchType,
			phoneOrder: phoneOrder,
			realName: realName,
			autoFilter: autoFilter,
			orderType: 2,
		};
		$scope.listData = [];
		AQ.callAQJSON("getShowTask.do", param, function(jsonData) {
			var result = jsonData.data;
			var data = [];
			for (var i = 0; i < result.length; ++i) {
				data.push({
					taskObj: result[i],
					taskId: result[i].taskId,
					searchType: DIC.SearchType[result[i].searchType],
					modifyPrice: DIC.TrueFalse(result[i].modifyPrice),
					phoneOrder: DIC.PhoneOrder[result[i].phoneOrder],
					orderText: result[i].phoneOrder == 0 ? "电脑访问" : "手机访问",
					rateTime: result[i].rateTime,
					checkBuyerIp: DIC.TrueFalse(result[i].checkBuyerIp),
					takeArea: DIC.TrueFalse(result[i].takeArea),
					rankLimit: result[i].rankLimit,
					realName: DIC.TrueFalse(result[i].realName),
					checkTaker: DIC.TrueFalse(result[i].checkTaker),
					compare: DIC.TrueFalse(result[i].compare),
					browseOthers: DIC.TrueFalse(result[i].browseOthers),
					collect: DIC.TrueFalse(result[i].collect),
					stayTime: DIC.TrueFalse(result[i].stayTime),
					talk: DIC.TrueFalse(result[i].talk),
					browseFoot: DIC.TrueFalse(result[i].browseFoot),
					address: DIC.TrueFalse(result[i].address),
					feibi: Utils.formatMoney(result[i].feibi),
					realYongjin: Utils.formatMoney(0.75 * result[i].feibi),
					totalMoney: Utils.formatMoney(result[i].totalMoney),
					price: Utils.formatMoney(result[i].price),
					amount: result[i].amount,
					showTime: result[i].showTime.substr(11),
					userName: result[i].userName,
					sellerNick: result[i].sellerNick,
					vipPic: DIC.VipPic[result[i].vip],
				});
			}
			$("div.m-list").find("#enpty-tips").remove();
			if(data.length == 0) {
				$("div.m-list").children("ul").append('<li id="enpty-tips" ' +
					'style="font-size:20px; color:#666; text-align:center;">'+
					'单子刚刚被抢完了哦！稍后刷新看看。<a href="task-center.html">接销量单</a></li>');
			}
			$scope.$apply(function () {
				$scope.listData = data;
				if(pageNo == 1 && needNewPagination) {	//创建分页
					var num_entries = jsonData.size;
					Utils.newPagination("Pagination", num_entries, function(page_index, jq) {
						if(page_index == 0 && needNewPagination) {
							needNewPagination = false;
							return;
						}
						refreshShowTask(page_index+1, sort, rateTime, searchType, phoneOrder, realName);
					}, 15);
				}
			});
		}, function(err, code) {
			console.log(err + "["+ code +"]");
		});
	}
	
	//获取充值记录
	function getCzRecords() {
		var param = {
			pageNo: 1,
			pageCount: 20,
		};
		AQ.callAQ("getCzRecords.do", param, function(result) {
			var data = [];
			for (var i = 1; i < result.length; ++i) {
				result[i].insertTime = result[i].insertTime.substr(0,11);
				result[i].tradeMoney = Utils.formatMoney(result[i].tradeMoney),
				data.push(result[i]);
			}
			$scope.$apply(function () {
				$scope.czListData = data;
				setTimeout(function() {
					jQuery(".slideTxtBox").slide({
						mainCell:".bd ul",
						autoPlay:true,
						effect:"topMarquee",
						vis:3,
						interTime:100,
						trigger:"click",
					});
				}, 10);
			});
		}, function(err, code) {
			console.log(err + "["+ code +"]");
		});
	}
	
	function getPublicForum() {
		AQ.callAQJSON("getIndexPublicForum.do", null, function(result) {
			var list = result.data;
			var data = [];
			for (var i=0; i<list.length; i++) {
				var forumEntry = list[i];
				data.push(forumEntry);
			}
			$scope.$apply(function () {
				$scope.publicForumList = data;
			});
		}, function(err, code) {
			console.log(err + "["+ code +"]");
		});
	}
	
	//按条件筛选任务
    $(".filter-box .line li").each(function(index, element) {		
		$(element).click(function() {
			$(element).siblings("li").css("color", "#666666");
			$(element).css("color", "#f50");
			var value = $(element).val();
			if(value == 100) {
				rateTime = 0;
			} else if(value == 101) {
				rateTime = 1;
			} else if(value == 102) {
				rateTime = 2;
			} else if(value == 103) {
				rateTime = 3;
			} else if(value == 104) {
				rateTime = 4;
			} else if(value == 105) {
				rateTime = 5;
			} else if(value == 106) {
				rateTime = 6;
			} else if(value == 107) {
				rateTime = 7;
			} else if(value == 108) {
				rateTime = 8;
			} else if(value == 201) {
				searchType = 1;
			} else if(value == 202) {
				searchType = 2;
			} else if(value == 203) {
				searchType = 3;
			} else if(value == 204) {
				searchType = 4;
			} else if(value == 205) {
				searchType = 5;
			} else if(value == 311) {
				phoneOrder = 0;
			} else if(value == 312) {
				phoneOrder = 1;
			} else if(value == 321) {
				realName = 0;
			} else if(value == 322) {
				realName = 1;
			}
			needNewPagination = true;
			refreshShowTask(1, sort, rateTime, searchType, phoneOrder, realName);
			location.hash = "#filter-box";
		});
    });
	
	//任务排序
	$(".sorts li").each(function(index, element) {
		$(element).click(function() {
            $(element).find("a").addClass("active").parent("li").siblings().find("a").removeClass("active");
			var value = $(element).val();
			if(value == 1) {
				sort = "t-desc";
			} else if(value == 2) {
				sort = "f-asc";
			} else if(value == 3) {
				sort = "f-desc";
			} else if(value == 4) {
				sort = "p-asc";
			} else if(value == 5) {
				sort = "p-desc";
			}
			refreshShowTask(1, sort, rateTime, searchType, phoneOrder, realName);
			location.hash = "#filter-box";
        });
    });
	
	//价格范围
	$(".prices .inputs .item .range-bt").click(function() {
		needNewPagination = true;
        refreshShowTask(1, sort, rateTime, searchType, phoneOrder, realName);
		location.hash = "#filter-box";
    });
	
	//开启/关闭任务自动过滤
	$(".auto-filter-box").click(function() {
        var temp = $(".auto-filter-box").val();
		if(temp == 0) {
			$(".auto-filter-box").css("background", "url(images/icon/icon_common_1.png) no-repeat -50px -350px");
			$(".auto-filter-box").val(1);
		} else if(temp == 1) {
			$(".auto-filter-box").css("background", "url(images/icon/icon_common_1.png) no-repeat 0px -350px");
			$(".auto-filter-box").val(0);
		}
		needNewPagination = true;
		refreshShowTask(1, sort, rateTime, searchType, phoneOrder, realName);
		location.hash = "#filter-box";
    });
	
	/* 购买飞币 */
	$(".fm-pay .btn-ok").click(function() {
        var money = 0;	//账户余额
        var siteFBPrice = 1.00;
		try {
			money = $.parseJSON($.cookie('User')).money;
			siteFBPrice = $.parseJSON($.cookie('User')).siteFBPrice;
		} catch(e) {
			console.log(e);
		}
		var fbnum = $.trim($("#buy-fb-num").val());
		var numReg = /^[1-9][0-9]{0,3}$/;
		if(!numReg.test(fbnum)) {
			Utils.msgDialog("请输入正确的飞币数量！");
			return;
		} else {
			var fbMoney = siteFBPrice * fbnum;
			if(fbMoney > money) {
				Utils.msgDialog("账户余额不足，请先充值！");
				return;
			}
		}
		var param = {
			feibi: fbnum,
		};
        AQ.callAQ("buyFeibi.do", param, function(result) {
        	Utils.msgDialog(result[0].info);
			Utils.refreshUserInfo();	//刷新用户信息
			$("#buy-fb-num").val("");
		}, function(err, code) {
			Utils.msgDialog(err + "["+ code +"]");
		});
    });
	
	/* 接单操作 */
	$scope.jieDan = function(taskObj) {
		ShowTaskData.taskObj = {
			orderType: taskObj.orderType,
			taskId: taskObj.taskId,
			searchType: DIC.SearchType[taskObj.searchType],
			phoneOrder: DIC.PhoneOrder[taskObj.phoneOrder],
			checkBuyerIp: DIC.TrueFalse(taskObj.checkBuyerIp),
			takeArea: DIC.TrueFalse(taskObj.takeArea),
			rankLimit: taskObj.rankLimit,
			realName: DIC.TrueFalse(taskObj.realName),
			checkTaker: DIC.TrueFalse(taskObj.checkTaker),
			compare: DIC.TrueFalse(taskObj.compare),
			browseOthers: DIC.TrueFalse(taskObj.browseOthers),
			collect: DIC.TrueFalse(taskObj.collect),
			stayTime: DIC.TrueFalse(taskObj.stayTime),
			talk: DIC.TrueFalse(taskObj.talk),
			browseFoot: DIC.TrueFalse(taskObj.browseFoot),
		};
		ShowTaskData.taskId = taskObj.taskId;
		selBuyerDialog();
	};
	
	
	
}]);



/*
显示选择买号对话框
*/
function selBuyerDialog() {
	$("#selBuyerDialog").each(function(index, element) {
        $(element).remove();
    });
	var content = $("<div id='selBuyerDialog'></div>").load("sel-buyer-dialog.html");
	var d = dialog({
			fixed: true,
			zIndex: 999999999,
    		title: '选择买号',
  	  		content: content,
//			okValue: '&nbsp;确&nbsp;定&nbsp;',
//    		ok: function () {
//				window.close();
//			},
//			cancelValue: '&nbsp;返&nbsp;回&nbsp;',
//			cancel: function() {}
	});
	d.width(660);
	d.height(400);
	d.showModal();
}
